#include "widget.h"
#include "ui_widget.h"
#include <QMenu>
#include <QAction>
#include <QFileDialog>
#include <QTextCodec>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //[0] create menu
    QMenu *menu = new QMenu(this);
    menu->setObjectName("m_menu");
    QMenu *menuTextCode = new QMenu("切换编码",menu);
    QActionGroup *agTextCode = new QActionGroup(menuTextCode);

    QAction *actOpenFile = new QAction("打开文本");
    QAction *actUTF8 = new QAction("UTF-8",agTextCode);
    actUTF8->setObjectName("utf8");
    QAction *actGBK = new QAction("GBK",agTextCode);
    actGBK->setObjectName("gbk");
    QAction *actBig5 = new QAction("Big5",agTextCode);
    actBig5->setObjectName("big5");

    //[1] connect
    connect(actOpenFile,SIGNAL(triggered(bool)),SLOT(slotOpenFile(bool)));
    connect(actUTF8,SIGNAL(triggered(bool)),this,SLOT(slotChangeTextCode(bool)));
    connect(actGBK,SIGNAL(triggered(bool)),this,SLOT(slotChangeTextCode(bool)));
    connect(actBig5,SIGNAL(triggered(bool)),this,SLOT(slotChangeTextCode(bool)));

    menu->addAction(actOpenFile);
    menuTextCode->addActions(agTextCode->actions());
    menu->addMenu(menuTextCode);



    ui->plainTextEdit->setContextMenuPolicy(Qt::NoContextMenu);


}

Widget::~Widget()
{
    delete ui;
}

void Widget::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu *m_menu = findChild<QMenu*>("m_menu");
    if(m_menu){
        m_menu->move(cursor().pos());
        m_menu->show();
    }
}

void Widget::reloadTextFile(QString codeName)
{
    if(m_filePath.isEmpty())
        return ;
    QFile file(m_filePath);
    file.open(QIODevice::ReadWrite);
    ui->plainTextEdit->clear();
    QByteArray ba = file.readAll();
    ui->plainTextEdit->appendPlainText(GetCorrectUnicode(ba,codeName));
}

void Widget::slotChangeTextCode(bool )
{
    if(!sender())
        return ;
    QString codeName = sender()->objectName();
    reloadTextFile(codeName);
}

void Widget::slotOpenFile(bool)
{
    m_filePath = QFileDialog::getOpenFileName(0,"选择文件","D:/","*.txt");
    if(m_filePath.isEmpty())
        return ;
    QFile file(m_filePath);
    file.open(QIODevice::ReadWrite);
    ui->plainTextEdit->clear();
    QByteArray ba = file.readAll();
    ui->plainTextEdit->appendPlainText(GetCorrectUnicode(ba));
}

QString Widget::GetCorrectUnicode(const QByteArray &text, QString codeName)
{
    QTextCodec::ConverterState state;
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QString strtext = codec->toUnicode( text.constData(), text.size(), &state);
    if (state.invalidChars > 0)
    {
        //原本字符的编码转成Unicode
        if(codeName == "utf8"){
            strtext = QTextCodec::codecForName("UTF-8")->toUnicode(text);
        }else if(codeName == "gbk"){
            strtext = QTextCodec::codecForName("GBK")->toUnicode(text);
        }else if(codeName == "big5"){
            strtext = QTextCodec::codecForName("Big5")->toUnicode(text);qDebug()<<"big";
        }
    }
    else
    {
        strtext = text;qDebug()<<"cc";
    }

    return strtext;
}

